﻿using System;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Drawing;
using NationalInstruments.VisaNS;

namespace MicroLED_Detecter
{
    public class AFG_1000
    {
        int flag_IsConnected = 0;// AFG是否处于连接状态
        int flag_IsOutput1 = 0;// AFG是否正在输出
        int flag_IsOutput2 = 0;
        private MessageBasedSession mbSession;
        string visaAddress = "USB0::0x0699::0x0353::1753041::INSTR";
        public int IsAFGConnected()
        {
            if (flag_IsConnected == 0)
                return 0;
            else
                return 1;
        }
        public int IsAFGOutput()
        {
            if (flag_IsOutput1 == 0 && flag_IsOutput2 == 0)
                return 0; 
            else
                return 1;
        }
        public int Connect() // 连接AFG
        {
            if (flag_IsConnected == 1)
                return 0;

            try
            {
                mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open(visaAddress);
                flag_IsConnected = 1;
                return 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            return 0;
        }
        public void disConnect() // 断开AFG
        {
            if (flag_IsConnected == 0)
                return;

            try
            {
                mbSession = null;
                flag_IsConnected = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            return;
        }
        private string GetWaveShape(int WaveShape)
        {
            string Shape = null;
            switch (WaveShape)
            {
                case 0:
                    Shape = "SINusoid";
                    break;
                case 1:
                    Shape = "SQUare";
                    break;
                case 2:
                    Shape = "RAMP";
                    break;
                case 3:
                    Shape = "PULSe";
                    break;
            }
            return Shape;
        }
        private String GetFrequencyUnit(int FrequencyUnit)
        {
            string frequency_unit = null;
            switch (FrequencyUnit)
            {
                case 0:
                    frequency_unit = "Hz";
                    break;
                case 1:
                    frequency_unit = "kHz";
                    break;
                case 2:
                    frequency_unit = "MHz";
                    break;
            }
            return frequency_unit;
        }
        private String GetOffsetUnit(int OffsetUnit)
        {
            string offset_unit = null;
            switch (OffsetUnit)
            {
                case 0:
                    offset_unit = "V";
                    break;
                case 1:
                    offset_unit = "mV";
                    break;
            }
            return offset_unit;
        }
        private String GetPhaseUnit(int PhaseUnit)
        {
            string phase_unit = null;
            switch (PhaseUnit)
            {
                case 0:
                    phase_unit = "RAD";
                    break;
                case 1:
                    phase_unit = "DEG";
                    break;
            }
            return phase_unit;
        }
        public void SaveParameters(int WaveShape, string Level, string Frequency, int FrequencyUnit, string Offset, int OffsetUnit, string Phase, int PhaseUnit)
        {
            if (flag_IsConnected == 0 || flag_IsOutput1 == 1)
                return;

            mbSession.Write("SOURce1:FUNCtion:SHAPe " + GetWaveShape(WaveShape));
            mbSession.Write("SOURce1:VOLTage:LEVel:IMMediate:AMPLitude " + Level + "Vpp");
            mbSession.Write("SOURce1:FREQuency:FIXed " + Frequency + GetFrequencyUnit(FrequencyUnit));
            mbSession.Write("SOURce1:VOLTage:LEVel:IMMediate:OFFSet " + Offset + GetOffsetUnit(OffsetUnit));
            mbSession.Write("SOURce1:PHASe:ADJust " + Phase + GetPhaseUnit(PhaseUnit));
        }
        public void SaveParameters2(int WaveShape, string Level, string Frequency, int FrequencyUnit, string Offset, int OffsetUnit, string Phase, int PhaseUnit)
        {
            if (flag_IsConnected == 0 || flag_IsOutput2 == 1)
                return;

            mbSession.Write("SOURce2:FUNCtion:SHAPe " + GetWaveShape(WaveShape));
            mbSession.Write("SOURce2:VOLTage:LEVel:IMMediate:AMPLitude " + Level + "Vpp");
            mbSession.Write("SOURce2:FREQuency:FIXed " + Frequency + GetFrequencyUnit(FrequencyUnit));
            mbSession.Write("SOURce2:VOLTage:LEVel:IMMediate:OFFSet " + Offset + GetOffsetUnit(OffsetUnit));
            mbSession.Write("SOURce2:PHASe:ADJust " + Phase + GetPhaseUnit(PhaseUnit));
        }
        public void Output1()
        {
            if(flag_IsConnected == 0 || flag_IsOutput1 == 1)
                return;

            try
            {
                mbSession.Write("OUTPut1:STATE ON");
                flag_IsOutput1 = 1;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        public void StopOutput1()
        {
            if (flag_IsConnected == 0 || flag_IsOutput1 == 0)
                return;

            try
            {
                mbSession.Write("OUTPut1:STATE OFF");
                flag_IsOutput1 = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
        public void Output2()
        {
            if (flag_IsConnected == 0 || flag_IsOutput2 == 1)
                return;

            try
            {
                mbSession.Write("OUTPut2:STATE ON");
                flag_IsOutput2 = 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        public void StopOutput2()
        {
            if (flag_IsConnected == 0 || flag_IsOutput2 == 0)
                return;

            try
            {
                mbSession.Write("OUTPut2:STATE OFF");
                flag_IsOutput2 = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
    }
}
